﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class AquacultureMethodEditControl : YuanlinUserControl,
        IReplyConverter<AquacultureMethodReply, GetOperation>
    {
        protected ValuedTextBox titleValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private AquacultureMethod aquacultureMethod;
        public int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetAquacultureMethodRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if (aquacultureMethod != null)
            {
                this.titleValuedTextBox.Value = aquacultureMethod.Title.Value;
            }   
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetAquacultureMethodListUrl(categoryType));
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (aquacultureMethod != null)
            {

                aquacultureMethod.Title.Value = CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
                aquacultureMethod.CategoryType.Value = categoryType;
                if(BusinessManager.UpdateAquacultureMethod(aquacultureMethod) ==-2)
                {
                    Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                    return;
                }
            }
            Page.Response.Redirect(SiteUrls.Instance().GetAquacultureMethodListUrl(categoryType));

        }


        #region IReplyConverter<AquacultureMethodMethodReply,GetOperation> Members

        public void ConvertReply(AquacultureMethodReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                aquacultureMethod = reply.Entity as AquacultureMethod;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}